package com.scm.biz.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.scm.biz.query.MsgNoticeQuery;
import com.scm.biz.query.OrderDynamicQuery;
import com.scm.biz.result.MsgNoticeResult;
import com.scm.biz.service.MsgNoticeService;
import com.scm.biz.service.SysUserService;
import com.scm.utils.Const;

@Controller
@RequestMapping("/work")
public class MsgNoticeAction {
	
	@Resource
	private MsgNoticeService msgNoticeService;
	@Resource
	private SysUserService sysUserService;
	
	@RequestMapping("/list")
	public ModelAndView getList(MsgNoticeQuery msgNoticeQuery,HttpServletRequest req){
		if(msgNoticeQuery.getPageNo()==0){
			msgNoticeQuery.setPageNo(1);
		}
		int  userId = (int)BaseAction.doGetSession("userId");

		msgNoticeQuery.setUserId(userId);
//		//当前用户的相关消息列表
		List<MsgNoticeResult> msgNoticeResults = msgNoticeService.getMsgNotice(msgNoticeQuery);
//		//当前用户及下级员工的前5条消息列表
		List<MsgNoticeResult> orderDynamicResults = msgNoticeService.getOrderDynamic5(userId);
		//所有订单数
		int allOrderTotal = msgNoticeService.getAllOrderTotal();
		//设计订单数
		int allDesignTotal = msgNoticeService.getDesignOrderTotal();
		//打样订单数
		int allProofingTotal = msgNoticeService.getProofingOrderTotal();
		//大货订单数
		int allProductionTotal = msgNoticeService.getProductionOrderTotal();
		int total = msgNoticeService.getTotal(msgNoticeQuery);
		msgNoticeQuery.setRecourdCount(total);
		ModelAndView mv;
		//status为1表示查询已处理消息列表，0或者空表示查询待处理消息列表
		if(msgNoticeQuery.getStatus()!=null&&msgNoticeQuery.getStatus()==1){
			mv = new ModelAndView("work/processed");
		}else{
			mv = new ModelAndView("work/list");
		}
		mv.addObject("msgNoticeResults", msgNoticeResults);
		mv.addObject("orderDynamicResults", orderDynamicResults);
		mv.addObject("allOrderTotal", allOrderTotal);
		mv.addObject("allDesignTotal", allDesignTotal);
		mv.addObject("allProofingTotal", allProofingTotal);
		mv.addObject("allProductionTotal", allProductionTotal);
		mv.addObject("splitPage", msgNoticeQuery.getSplitPage());
		mv.addObject("url", Const.getPath(req));
		return mv;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/orderDynamicList")
	public ModelAndView getOrderDynamicList(HttpServletRequest req){
		OrderDynamicQuery orderDynamicQuery = new OrderDynamicQuery();
		int  userId = (int)BaseAction.doGetSession("userId");
		//获取当前用户的下级Id
		List<Integer> ids = null;
		ids.add(userId);
		orderDynamicQuery.setIds(ids);
		if(orderDynamicQuery.getPageNo()==0){
			orderDynamicQuery.setPageNo(1);
		}
		ModelAndView mv = new ModelAndView ("work/order_dynamic");
		//当前用户及下级员工的订单动态列表
		List<MsgNoticeResult> orderDynamicResults = msgNoticeService.getOrderDynamic(orderDynamicQuery);
		//总数
		int total = msgNoticeService.getOrderDynamicTotal(orderDynamicQuery);
		orderDynamicQuery.setRecourdCount(total);
		mv.addObject("orderDynamicResults", orderDynamicResults);
		mv.addObject("splitPage", orderDynamicQuery.getSplitPage());
		mv.addObject("url", Const.getPath(req));
		return mv;
	}
	
	@RequestMapping("/modifyStatus")
	public ModelAndView modifyStatus(Long id){
		ModelAndView mv = new ModelAndView("redirect:list");
		msgNoticeService.modifyStatus(id);
		return mv;
	}
}
